package com.sikadwafastdaisfondturbo.pretloan.utils.helper

import com.sikadwafastdaisfondturbo.pretloan.R
import com.sikadwafastdaisfondturbo.pretloan.app.App
import org.json.JSONObject
import java.util.Calendar

/**
 * Sikadwa
 * @Author wwdeng
 * @CreateTime: 2024/5/24 10:13
 * @Annotation：日期帮助类
 */
object DateHelper {

    /**
     * 月份
     */
    private val MONTH = arrayListOf(R.string.month1,R.string.month2,R.string.month3,R.string.month4,R.string.month5,R.string.month6,R.string.month7,R.string.month8,R.string.month9,R.string.month10,R.string.month11,R.string.month12)

    /**
     * 获取月份列表
     */
    fun getMonthList(): ArrayList<JSONObject>{
        val monthList = arrayListOf<JSONObject>()
        MONTH.forEach {
            val item  = JSONObject()
            item.put("month",App.instance().getString(it))
            monthList.add(item)
        }
        return monthList
    }

    /**
     * 根据月份获取的天列表
     */
    fun getDayList(year: Int,month: Int): ArrayList<JSONObject>{
        val dayList = arrayListOf<JSONObject>()
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month - 1)
        val daysCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        for (i in 0..<daysCount){
            val item  = JSONObject()
            item.put("year",year)
            item.put("month",month)
            item.put("day",i+1)
            var isEnabled = true
            //距当前日期18年之内的日期不可选
            if (year > getEighteenYearsBefore()){
                isEnabled = false
            } else {
                if (year == getEighteenYearsBefore()){
                    if (month > getCurtMonth()){
                        isEnabled = false
                    } else {
                        if (month == getCurtMonth()){
                            if (i+1 > getCurtDay()){
                                isEnabled = false
                            }
                        }
                    }
                }
            }
            item.put("isEnabled",isEnabled)

            dayList.add(item)
        }
        return dayList
    }

    /**
     * 获取指定日期是星期几
     *  1 - 7 ： 星期日 - 星期六
     */
    fun getDayOfWeek(year: Int,month: Int,day: Int): Int{
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month - 1)
        calendar.set(Calendar.DAY_OF_MONTH,day)
        return calendar.get(Calendar.DAY_OF_WEEK) - 1
    }

    /**
     * 获取当前年份
     */
    fun getCurtYear(): Int{
        return Calendar.getInstance().get(Calendar.YEAR)
    }

    /**
     * 获取当前月份
     */
    fun getCurtMonth(): Int{
        return Calendar.getInstance().get(Calendar.MONTH) + 1
    }

    /**
     * 获取当前日期
     */
    fun getCurtDay(): Int{
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
    }

    /**
     * 获取18年前
     */
    fun getEighteenYearsBefore(): Int {
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.YEAR, -18)
        return calendar.get(Calendar.YEAR)
    }
}